package org.imaginedays.androidall.widget;

import org.imaginedays.androidall.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.support.v4.util.LruCache;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.TextView;


/**
 * 自定义带字体属性的 textView
 * @author imaginedays
 *
 */
public class TypefaceTextView extends TextView {
	
	private static LruCache<String, Typeface> sTypefaceCache = new LruCache<String, Typeface>(12);

	public TypefaceTextView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public TypefaceTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		  TypedArray a = context.getTheme().obtainStyledAttributes(attrs,  R.styleable.TypefaceTextView, 0, 0);
		   try {            
			   String typefaceName = a.getString(R.styleable.TypefaceTextView_typeface);     
			   if ( !TextUtils.isEmpty(typefaceName)) {               
				   Typeface typeface = sTypefaceCache.get(typefaceName);  
				   if (typeface == null) {                  
					   typeface = Typeface.createFromAsset(context.getAssets(),   
							   String.format("fonts/DFGirl.ttf", typefaceName));                                        // Cache the Typeface object                   
		   sTypefaceCache.put(typefaceName, typeface);                }              
				   setTypeface(typeface);            }     
			   }catch(Exception e){
				   
			   } finally {  
			   a.recycle();        
			   }
	}

	public TypefaceTextView(Context context) {
		super(context);
	}

}
